//
//  ViewController.m
//  21--Network
//
//  Created by 冀永金 on 16/8/11.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *networkImage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg
    // Do any additional setup after loading the view, typically from a nib.
}
// 同步
- (IBAction)synContent:(id)sender {
    NSMutableURLRequest *requestImage = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"]];
    requestImage.HTTPMethod = @"GET";
    NSURLResponse *response = nil;
    NSError *error = nil;
    // 同步请求
    // 参数1：请求request
//  参数2：response指针
    // 参数3：response的地址
     NSData *data = [NSURLConnection sendSynchronousRequest:requestImage returningResponse:&response error:&error];
    if (data) {
        _networkImage.image = [UIImage imageWithData:data];
    }
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"dddd");
}
// 异步
- (IBAction)asynContent:(id)sender {
    NSMutableURLRequest *requestImage = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"]];
    requestImage.HTTPMethod = @"GET";
     [NSURLConnection sendAsynchronousRequest:requestImage queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
         _networkImage.image = [UIImage imageWithData:data];
     }];
}
- (IBAction)urlSessionAsync:(id)sender {
    // 网络请求
    // 参数1：request
    // 参数2：Block块。CallBack回调
    // 注意：Session的回调中处于异步线程，需要返回主线程刷新UI(图片赋值，TableView CollectionView的刷新)
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSMutableURLRequest *requestImage = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"]];
    requestImage.HTTPMethod = @"GET";
  NSURLSessionDataTask *dataTask =  [urlSession dataTaskWithRequest:requestImage completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
      // 主线程加载图片
      dispatch_async(dispatch_get_main_queue(), ^{
          _networkImage.image = [UIImage imageWithData:data];
      });
   }];
    [dataTask resume];
}
- (IBAction)post:(id)sender {
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *imageURL = [NSURL URLWithString:@"http://open3.bantangapp.com/base/app/init"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"POST";
    // ? 后面
    imageURLRequest.HTTPBody = [@"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17" dataUsingEncoding:NSUTF8StringEncoding];
    // URLSession, 网络请求
    // 参数1: request
    // 参数2: Block块, CallBack回调
    // 注意: Session的回调中处于异步线程, 需要返回主线程刷新UI(图片赋值, TableView CollectionView的刷新)
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 主线程执行图片加载
        dispatch_async(dispatch_get_main_queue(), ^{
            //            _networkImage.image = [UIImage imageWithData:data];
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@", result);
        });
    }];
    // 让 dataTask 执行
    [dataTask resume];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
